using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_DanhSachLoaiSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        Criteria cri = new Criteria();
        int total = 0;
        if (txt_TenLoai.Text != "")
        cri.add(Condition.AND, tbLoaiSanPhamEntities.tbLoaiSanPhanEntitiesColumns.TenLoai.ToString(), Condition.LIKE, txt_TenLoai.Text);
        tbLoaiSanPhamEntities.tbLoaiSanPhamEntitiesCollection loaiSPCollection = tbLoaiSanPhamBUS.Get(cri.Criter, pager.CurrentIndex, pager.PageSize,
        tbLoaiSanPhamEntities.tbLoaiSanPhanEntitiesColumns.MaLoai.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = loaiSPCollection;
        Repeater1.DataBind();
        pager.ItemCount = total;
        if (total <= pager.PageSize)
            pager.Visible = false;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
              || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hyp_TenLoai = (HyperLink)e.Item.FindControl("hyp_TenLoai");
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            Literal lit_MaLoai = (Literal)e.Item.FindControl("lit_MaLoai");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");

           tbLoaiSanPhamEntities loaiSP = (tbLoaiSanPhamEntities)e.Item.DataItem;

            
            lit_MaLoai.Text =loaiSP.MaLoai.ToString();
            hpl_Edit.NavigateUrl = "EditLoaiSanPham.aspx?id=" + loaiSP.MaLoai.ToString();
            hf_id.Value =loaiSP.MaLoai.ToString();
           
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = Convert.ToInt32(e.CommandArgument);
        LoadData();
    }

    protected void lbt_Delete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("cb_Xoa");
            if (chk.Checked == true)
            {
                HiddenField hf_id = (HiddenField)item.FindControl("hf_id");
                tbLoaiSanPhamBUS.Delete(int.Parse(hf_id.Value));
            }
        }
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbLoaiSanPhamEntities loaiSP = tbLoaiSanPhamBUS.GetByID(id);
                //if (sanPham.HinhMinhHoa != "no_images.jpg")
                //{
                //    System.IO.File.Delete(Server.MapPath("~/Photos/" + sanPham.HinhMinhHoa));
                //    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + sanPham.HinhMinhHoa));
                //}
                tbLoaiSanPhamBUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
